package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysPlace;
import com.ruoyi.system.domain.vo.PlaceCascaderVo;
import com.ruoyi.system.domain.vo.SelectByPlaceLevelVo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.mapper.SysPlaceMapper;
import com.ruoyi.system.service.ISysPlaceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 地区管理Service业务层处理
 *
 * @author linyang
 * @date 2024-01-17
 */
@Service
public class SysPlaceServiceImpl implements ISysPlaceService {
    @Resource
    private SysPlaceMapper sysPlaceMapper;


    @Resource
    private SysDictTypeMapper sysDictTypeMapper;


    @Resource
    private SysDictDataMapper sysDictDataMapper;


    /**
     * 查询地区管理
     *
     * @param id 地区管理主键
     * @return 地区管理
     */
    @Override
    public SysPlace selectSysPlaceById(Long id) {
        return sysPlaceMapper.selectSysPlaceById(id);
    }

    /**
     * 查询地区管理列表
     *
     * @param sysPlace 地区管理
     * @return 地区管理
     */
    @Override
    public List<SysPlace> selectSysPlaceList(SysPlace sysPlace) {
        return sysPlaceMapper.selectSysPlaceList(sysPlace);
    }


    /**
     * 根据关键字查询地区列表
     *
     * @param keyword
     * @return
     */
    public List<SysPlace> selectSearchSysPlaceList(String keyword) {
        LambdaQueryWrapper<SysPlace> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(SysPlace::getName, keyword);
        return sysPlaceMapper.selectList(lambdaQueryWrapper);
    }


    /**
     * 筛选出FullName为空的地区列表
     *
     * @return
     */
    public List<SysPlace> selectEmptyFullNameSysPlaceList() {
        return sysPlaceMapper.selectEmptyFullNameSysPlaceList();
    }


    /**
     * 获取定时任务要抓取的地区列表
     *
     * @return
     */
    public List<SysPlace> selectTaskSysPlaceList() {
        return sysPlaceMapper.selectTaskSysPlaceList();
    }

    /**
     * 新增地区管理
     *
     * @param sysPlace 地区管理
     * @return 结果
     */
    @Override
    public int insertSysPlace(SysPlace sysPlace) {
        sysPlace.setCreateTime(DateUtils.getNowDate());
        return sysPlaceMapper.insertSysPlace(sysPlace);
    }

    /**
     * 修改地区管理
     *
     * @param sysPlace 地区管理
     * @return 结果
     */
    @Override
    public int updateSysPlace(SysPlace sysPlace) {
        sysPlace.setUpdateTime(DateUtils.getNowDate());
        return sysPlaceMapper.updateSysPlace(sysPlace);
    }

    /**
     * 批量删除地区管理
     *
     * @param ids 需要删除的地区管理主键
     * @return 结果
     */
    @Override
    public int deleteSysPlaceByIds(Long[] ids) {
        return sysPlaceMapper.deleteSysPlaceByIds(ids);
    }

    /**
     * 删除地区管理信息
     *
     * @param id 地区管理主键
     * @return 结果
     */
    @Override
    public int deleteSysPlaceById(Long id) {
        return sysPlaceMapper.deleteSysPlaceById(id);
    }


    /**
     * @param vo
     * @return
     */
    @Override
    public List<PlaceCascaderVo> selectByPlaceLevel(SelectByPlaceLevelVo vo) {
        LambdaQueryWrapper<SysPlace> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (vo.getLevel().equals(0)) {
            List<SysDictData> sysDictData = sysDictDataMapper.selectDictDataByType("area");
            return sysDictData.stream().map(x -> {
                PlaceCascaderVo placeCascaderVo = new PlaceCascaderVo();
                placeCascaderVo.setLabel(x.getDictValue());
                placeCascaderVo.setValue(x.getDictValue());
                placeCascaderVo.setLeaf(!(this.selectChildCountByProvince(x.getDictValue()) > 0));
                return placeCascaderVo;
            }).collect(Collectors.toList());
        } else if (vo.getLevel().equals(1)) {
            lambdaQueryWrapper.isNotNull(SysPlace::getProvince).ne(SysPlace::getProvince, "").eq(SysPlace::getProvince, vo.getName()).groupBy(SysPlace::getProvince, SysPlace::getCity).select(SysPlace::getProvince, SysPlace::getCity).orderByAsc(SysPlace::getProvince, SysPlace::getCity);
            List<Map<String, Object>> maps = sysPlaceMapper.selectMaps(lambdaQueryWrapper);
            return maps.stream().map(x -> {
                PlaceCascaderVo placeCascaderVo = new PlaceCascaderVo();
                placeCascaderVo.setLabel(x.get("city").toString());
                placeCascaderVo.setValue(x.get("city").toString());
                placeCascaderVo.setLeaf(!(this.selectChildCountByCity(x.get("province").toString(), x.get("city").toString()) > 0));
                return placeCascaderVo;
            }).collect(Collectors.toList());

        } else if (vo.getLevel().equals(2)) {
            String[] places = vo.getName().split(",");
            lambdaQueryWrapper.isNotNull(SysPlace::getProvince).ne(SysPlace::getProvince, "").eq(SysPlace::getProvince, places[0]).eq(SysPlace::getCity, places[1]).groupBy(SysPlace::getProvince, SysPlace::getCity, SysPlace::getCounty).select(SysPlace::getProvince, SysPlace::getCity, SysPlace::getCounty).orderByAsc(SysPlace::getProvince, SysPlace::getCity, SysPlace::getCounty);
            List<Map<String, Object>> maps = sysPlaceMapper.selectMaps(lambdaQueryWrapper);
            return maps.stream().map(x -> {
                PlaceCascaderVo placeCascaderVo = new PlaceCascaderVo();
                placeCascaderVo.setLabel(x.get("county").toString());
                placeCascaderVo.setValue(x.get("county").toString());
                placeCascaderVo.setLeaf(true);
                return placeCascaderVo;
            }).collect(Collectors.toList());
        }
        return new ArrayList<PlaceCascaderVo>();
    }


    /**
     * 根据省份查询子节点数量
     *
     * @param provinceName
     * @return
     */
    private Long selectChildCountByProvince(String provinceName) {
        LambdaQueryWrapper<SysPlace> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlace::getProvince, provinceName).isNotNull(SysPlace::getCity).ne(SysPlace::getCity, "");
        return sysPlaceMapper.selectCount(lambdaQueryWrapper);
    }


    /**
     * 根据城市查询子节点数量
     *
     * @param provinceName
     * @param cityName
     * @return
     */
    private Long selectChildCountByCity(String provinceName, String cityName) {
        LambdaQueryWrapper<SysPlace> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysPlace::getProvince, provinceName).eq(SysPlace::getCity, cityName).isNotNull(SysPlace::getCounty).ne(SysPlace::getCounty, "");
        return sysPlaceMapper.selectCount(lambdaQueryWrapper);
    }

}
